Intersection


Introduction

Structured Query Language (SQL) is a powerful tool used to manage and manipulate data within relational databases. One of the fundamental operations in SQL is the ability to retrieve data from multiple tables based on certain conditions. The 'INTERSECT' operator is a key component that allows users to find common records between two or more SELECT statements.

What is the 'INTERSECT' Operator?

The 'INTERSECT' operator in SQL is used to combine the results of two or more SELECT statements and retrieve only the rows that appear in all result sets. It essentially finds the intersection of data sets, returning rows that are common across all SELECT statements.


Syntax of 'INTERSECT' :

SELECT column1, column2, ...

FROM table1

INTERSECT

SELECT columnA, columnB, ...

FROM table2;

Here, 'column1', 'column2', etc., represent the columns selected from 'table1', and 'columnA', 'columnB', etc., are columns selected from 'table2'. The 'INTERSECT' operator combines the results of both SELECT statements and returns only the rows that are common between them.


Example:

Let's consider a scenario where we have two tables: 'employees' and 'departments'. The 'employees' table contains information about employees, including their ID, name, and department ID. The 'departments' table holds details about different departments, including their ID and name.


employees table:

emp_id

emp_name

department_id

1

Alice

101

2

Bob

102

3

Charlie

101

4

David

103

5

Eve

101


departments table:

dept_id

dept_name

101

Sales

102

Marketing

103

Operations


Suppose we want to find employees who work in departments with IDs 101 and 102. We can use the 'INTERSECT' operator to achieve this:

SELECT emp_id, emp_name, department_id

FROM employees

WHERE department_id = 101


INTERSECT


SELECT emp_id, emp_name, department_id

FROM employees

WHERE department_id = 102;


The result of this query will be an empty set since no employee exists in both departments with IDs 101 and 102 simultaneously. However, if we had common employees between these departments, the query would return those employees' details.

Example:

employees table

emp_id

emp_name

department_id

1

Alice

101

2

Bob

102

3

Charlie

101

4

David

103

5

Eve

102

6

Frank

101

7

Grace

102

departments table

dept_id

dept_name

101

Sales

102

Marketing

103

Operations

Now, if we want to find employees who work in both Sales (department_id = 101) and Marketing (department_id = 102), we can use the 'INTERSECT' operator:

SELECT emp_id, emp_name, department_id

FROM employees

WHERE department_id = 101


INTERSECT


SELECT emp_id, emp_name, department_id

FROM employees

WHERE department_id = 102;

The result of this query would be an empty set because, in this example, there are no employees who work in both the Sales and Marketing departments simultaneously.

If we had an employee who worked in both departments, say if 'Alice' worked in both Sales and Marketing, the query would return her details:
employees table

emp_id

emp_name

department_id

1

Alice

101

2

Bob

102

3

Charlie

101

4

David

103

5

Eve

102

6

Frank

101

7

Grace

102


In this case, if 'Alice' is assigned to both departments, the 'INTERSECT' query would yield:

emp_id

emp_name

department_id

1

Alice

101

This output would indicate that 'Alice' is the only employee who works in both the Sales and Marketing departments based on the given criteria.

Important Points to Consider

  • The 'INTERSECT' operator only returns distinct rows that appear in all result sets. Duplicate rows are eliminated.
  • The number and order of columns selected in each SELECT statement within the 'INTERSECT' operator must match.
  • The data types of corresponding columns from different SELECT statements must be compatible for the 'INTERSECT' operation to work correctly.

Conclusion

The 'INTERSECT' operator in SQL allows users to find common rows between multiple SELECT statements, making it a valuable tool for data analysis and retrieval. By understanding its usage and syntax, SQL users can effectively extract specific intersecting data from different tables, enabling more precise querying and analysis within relational databases.